Sensor Fusion for Kinetis MCUs (ISSDK/KSDK version)
driver_FXLS8471Q.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, NXP Semiconductor
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*! \file driver_FXLS8471Q.c
31  \brief Provides init() and read() functions for the FXLS8471Q 3-axis accel.
32 
33  Supports both I2C and SPI Interfaces. Supply sensor address=0x00 when when
34  installing sensor for SPI. Supply I2C address otherwise.
35 */
36 
37 #include "board.h" // generated by Kinetis Expert. Long term - merge sensor_board.h into this file
38 #include "sensor_fusion.h" // Sensor fusion structures and types
39 #include "sensor_drv.h" // ISSDK - contains sensor state and error definitions
40 #include "register_io_spi.h" // ISSDK - declares low-level interface functions for single-location reading
41 #include "sensor_io_spi.h" // ISSDK - declares low-level interface functions for multi-location reading
42 #include "sensor_io_i2c.h" // Required for registerreadlist_t / registerwritelist_t declarations
43 #include "fxls8471q.h"
44 #include "fxls8471q_drv.h" // describes the FXLS8471Q ISSDK driver interface and structures
45 #include "drivers.h" // Device specific drivers supplied by NXP (can be replaced with user drivers)
46 #define FXLS8471Q_COUNTSPERG 8192.0
47 #define FXLS8471Q_ACCEL_FIFO_SIZE 32
48 
49 // Command definition to read the WHO_AM_I value.
50 const registerreadlist_t FXLS8471Q_WHO_AM_I_READ[] =
51 {
52  { .readFrom = FXLS8471Q_WHO_AM_I, .numBytes = 1 }, __END_READ_DATA__
53 };
54 
55 // Command definition to read the number of entries in the accel FIFO.
56 const registerreadlist_t FXLS8471Q_F_STATUS_READ[] =
57 {
58  { .readFrom = FXLS8471Q_F_STATUS, .numBytes = 1 }, __END_READ_DATA__
59 };
60 
61 // Command definition to read the number of entries in the accel FIFO.
62 registerreadlist_t FXLS8471Q_DATA_READ[] =
63 {
64  { .readFrom = FXLS8471Q_OUT_X_MSB, .numBytes = 6 }, __END_READ_DATA__
65 };
66 
67 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
68 const registerwritelist_t FXLS8471Q_Initialization[] =
69 {
70  // write 0000 0000 = 0x00 to CTRL_REG1 to place FXLS8471 into standby
71  // [7-1] = 0000 000
72  // [0]: active=00
73  { FXLS8471Q_CTRL_REG1, 0x00, 0x00 },
74 
75  // Not applicable to the 8452/3.
76  // OK: write 0100 0000 = 0x40 to F_SETUP to enable FIFO in continuous (circular) mode
77  // [7-6]: F_MODE[1-0]=01 for FIFO continuous mode
78  // [5-0]: F_WMRK[5-0]=000000 for no FIFO watermark
79  { FXLS8471Q_F_SETUP, FXLS8471Q_F_SETUP_F_MODE_FIFOMOSTRECENT, 0x00 },
80 
81  // write 0000 0001 = 0x01 to XYZ_DATA_CFG register to set g range
82  // [7-5]: reserved=000
83  // [4]: HPF_OUT=0
84  // [3-2]: reserved=00
85  // [1-0]: FS=01 for +/-4g: 512 counts / g = 8192 counts / g after 4 bit left shift
86  { FXLS8471Q_XYZ_DATA_CFG, FXLS8471Q_XYZ_DATA_CFG_FS_FS_RANGE_4G, 0x00 },
87 
88  // write 0000 0010 = 0x02 to CTRL_REG2 to set MODS bits
89  // [7]: ST=0: self test disabled
90  // [6]: RST=0: reset disabled
91  // [5]: unused
92  // [4-3]: SMODS=00
93  // [2]: SLPE=0: auto sleep disabled
94  // [1-0]: mods=10 for high resolution (maximum over sampling)
95  { FXLS8471Q_CTRL_REG2, FXLS8471Q_CTRL_REG2_MODS_HIGHRES, 0x00 },
96 
97  // write 00XX X001 to CTRL_REG1 to set data rate and exit standby
98  // [7-6]: aslp_rate=00
99  // [5-3]: dr=111 for 1.56Hz data rate giving 0x39
100  // [5-3]: dr=110 for 6.25Hz data rate giving 0x31
101  // [5-3]: dr=101 for 12.5Hz data rate giving 0x29
102  // [5-3]: dr=100 for 50Hz data rate giving 0x21
103  // [5-3]: dr=011 for 100Hz data rate giving 0x19
104  // [5-3]: dr=010 for 200Hz data rate giving 0x11
105  // [5-3]: dr=001 for 400Hz data rate giving 0x09
106  // [5-3]: dr=000 for 800Hz data rate giving 0x01
107  // [2]: unused=0
108  // [1]: f_read=0 for normal 16 bit reads
109  // [0]: active=1 to take the part out of standby and enable sampling
110 #if (ACCEL_ODR_HZ <= 1)
111  { FXLS8471Q_CTRL_REG1, FXLS8471Q_CTRL_REG1_MODE_ACTIVE | FXLS8471Q_CTRL_REG1_DR_1DOT56HZ, 0x00 }, // select 1.5625Hz ODR,
112 #elif (ACCEL_ODR_HZ <= 6)
113  { FXLS8471Q_CTRL_REG1, FXLS8471Q_CTRL_REG1_MODE_ACTIVE | FXLS8471Q_CTRL_REG1_DR_6DOT25HZ, 0x00 }, // select 6.25Hz ODR
114 #elif (ACCEL_ODR_HZ <= 12)
115  { FXLS8471Q_CTRL_REG1, FXLS8471Q_CTRL_REG1_MODE_ACTIVE | FXLS8471Q_CTRL_REG1_DR_12DOT5HZ, 0x00 }, // select 12.5Hz ODR
116 #elif (ACCEL_ODR_HZ <= 50)
117  { FXLS8471Q_CTRL_REG1, FXLS8471Q_CTRL_REG1_MODE_ACTIVE | FXLS8471Q_CTRL_REG1_DR_50HZ, 0x00 }, // select 50Hz ODR
118 #elif (ACCEL_ODR_HZ <= 100)
119  { FXLS8471Q_CTRL_REG1, FXLS8471Q_CTRL_REG1_MODE_ACTIVE | FXLS8471Q_CTRL_REG1_DR_100HZ, 0x00 }, // select 100Hz ODR
120 #elif (ACCEL_ODR_HZ <= 200)
121  { FXLS8471Q_CTRL_REG1, FXLS8471Q_CTRL_REG1_MODE_ACTIVE | FXLS8471Q_CTRL_REG1_DR_200HZ, 0x00 }, // select 200Hz ODR
122 #elif (ACCEL_ODR_HZ <= 400)
123  { FXLS8471Q_CTRL_REG1, FXLS8471Q_CTRL_REG1_MODE_ACTIVE | FXLS8471Q_CTRL_REG1_DR_400HZ, 0x00 }, // select 400Hz ODR
124 #else
125  { FXLS8471Q_CTRL_REG1, FXLS8471Q_CTRL_REG1_MODE_ACTIVE , 0x00 }, // select 800Hz ODR
126 #endif
127  __END_WRITE_DATA__
128 };
129 
130 // All sensor drivers and initialization functions have the same prototype.
131 // sfg is a pointer to the master "global" sensor fusion structure.
132 // sensor = pointer to linked list element used by the sensor fusion subsystem to specify required sensors
133 
134 // sfg = pointer to top level (generally global) data structure for sensor fusion
136 {
137  GENERIC_DRIVER_GPIO *pGPIODriver = &Driver_GPIO_KSDK;
138  int32_t status;
139  uint8_t reg;
140  if (sensor->addr == SPI_ADDR) {
141  // Initialize the Slave Select Pin.
142  pGPIODriver->pin_init(&FXLS8471_SPI_CS, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
143  // Initialize SPI parameters used by ISSDK SPI routines
144  sensor->slaveParams.pReadPreprocessFN = FXLS8471Q_SPI_ReadPreprocess;
145  sensor->slaveParams.pWritePreprocessFN = FXLS8471Q_SPI_WritePreprocess;
146  sensor->slaveParams.pTargetSlavePinID = &FXLS8471_SPI_CS;
147  sensor->slaveParams.spiCmdLen = FXLS8471Q_SPI_CMD_LEN;
148  sensor->slaveParams.ssActiveValue = FXLS8471Q_SS_ACTIVE_VALUE;
149  // Read the WhoAmI
150  status = Register_SPI_Read(sensor->bus_driver, &(sensor->slaveParams), FXLS8471Q_WHO_AM_I, 1, &reg);
151  } else {
152  status = Register_I2C_Read(sensor->bus_driver, sensor->addr, FXLS8471Q_WHO_AM_I, 1, &reg);
153  }
154  if (status==SENSOR_ERROR_NONE) {
155  sfg->Accel.iWhoAmI = reg;
156  if (reg!=FXLS8471Q_WHO_AM_I_WHOAMI_VALUE) return(SENSOR_ERROR_INIT);
157  } else {
158  return(status);
159  }
160 
161  // Configure and start the FXLS8471Q sensor. This does multiple register writes
162  if (sensor->addr == SPI_ADDR)
163  status = Sensor_SPI_Write(sensor->bus_driver, &(sensor->slaveParams), FXLS8471Q_Initialization );
164  else
165  status = Sensor_I2C_Write(sensor->bus_driver, sensor->addr, FXLS8471Q_Initialization );
166 
167  // Stash some needed constants in the SF data structure for this sensor
168  sfg->Accel.iCountsPerg = FXLS8471Q_COUNTSPERG;
169  sfg->Accel.fgPerCount = 1.0F / FXLS8471Q_COUNTSPERG;
170 
171  sensor->isInitialized = F_USING_ACCEL;
172  sfg->Accel.isEnabled = true;
173 
174  return (status);
175 }
176 
178 {
179  uint8_t Data_Buffer[6 * FXLS8471Q_ACCEL_FIFO_SIZE]; // I2C read buffer
180  int8_t status=0; // I2C transaction status
181  int8_t j; // scratch
182  uint16_t sensor_fifo_count = 1;
183  int16_t sample[3];
184 
185  if(sensor->isInitialized != F_USING_ACCEL)
186  {
187  return SENSOR_ERROR_INIT;
188  }
189 
190  // read the F_STATUS register (mapped to STATUS) and extract number of measurements available (lower 6 bits)
191  if (sensor->addr == SPI_ADDR)
192  status = Sensor_SPI_Read(sensor->bus_driver, &(sensor->slaveParams), FXLS8471Q_F_STATUS_READ, Data_Buffer );
193  else
194  status = Sensor_I2C_Read(sensor->bus_driver, sensor->addr, FXLS8471Q_F_STATUS_READ, Data_Buffer );
195 
196  sensor_fifo_count = Data_Buffer[0] & 0x3F;
197  if (status==SENSOR_ERROR_NONE) {
198  // return if there are no measurements in the sensor FIFO.
199  // this will only occur when the FAST_LOOP_HZ equals or exceeds ACCEL_ODR_HZ
200  if (sensor_fifo_count == 0) return SENSOR_ERROR_READ;
201  } else {
202  return(status);
203  }
204 
205  FXLS8471Q_DATA_READ->numBytes = 6 * sensor_fifo_count;
206  if (sensor->addr == SPI_ADDR)
207  status = Sensor_SPI_Read(sensor->bus_driver, &sensor->slaveParams, FXLS8471Q_DATA_READ, &(Data_Buffer[0]) );
208  else
209  status = Sensor_I2C_Read(sensor->bus_driver, sensor->addr, FXLS8471Q_DATA_READ, &(Data_Buffer[0]) );
210 
211  if (status==SENSOR_ERROR_NONE) {
212  for (j=0; j<sensor_fifo_count; j++) {
213  sample[CHX] = (Data_Buffer[6*j ] << 8) | Data_Buffer[6*j + 1] ;
214  sample[CHY] = (Data_Buffer[6*j + 2] << 8) | Data_Buffer[6*j + 3] ;
215  sample[CHZ] = (Data_Buffer[6*j + 4] << 8) | Data_Buffer[6*j + 5] ;
216  conditionSample(sample); // truncate negative values to -32767
217  addToFifo((FifoSensor*) &(sfg->Accel), ACCEL_FIFO_SIZE, sample);
218  }
219  }
220 
221  return (status);
222 }
223 
224 
225 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
226 const registerwritelist_t FXLS8471Q_IDLE[] =
227 {
228  // Set ACTIVE = other bits unchanged
229  { FXLS8471Q_CTRL_REG1, 0x00, 0x01 },
230  __END_WRITE_DATA__
231 };
232 
233 // FXLS8471Q_Idle places the sensor into Standby mode (wakeup time = 2/ODR+1ms)
235 {
236  int32_t status;
237  if(sensor->isInitialized == F_USING_ACCEL) {
238  if (sensor->addr == SPI_ADDR) {
239  status = Sensor_SPI_Write(sensor->bus_driver, &(sensor->slaveParams), FXLS8471Q_IDLE );
240  } else {
241  status = Sensor_I2C_Write(sensor->bus_driver, sensor->addr, FXLS8471Q_IDLE );
242  }
243  sensor->isInitialized = 0;
244  sfg->Accel.isEnabled = false;
245  } else {
246  return SENSOR_ERROR_INIT;
247  }
248  return status;
249 }
#define FXLS8471Q_COUNTSPERG
#define CHY
Used to access Y-channel entries in various data data structures.
Definition: sensor_fusion.h:77
const registerwritelist_t FXLS8471Q_Initialization[]
void addToFifo(FifoSensor *sensor, uint16_t maxFifoSize, int16_t sample[3])
addToFifo is called from within sensor driver read functions
The top level fusion structure.
void * bus_driver
should be of type (ARM_DRIVER_I2C* for I2C-based sensors, ARM_DRIVER_SPI* for SPI) ...
int8_t FXLS8471Q_Read(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define ACCEL_FIFO_SIZE
FXOS8700 (accel), MMA8652, FXLS8952 all have 32 element FIFO.
The FifoSensor union allows us to use common pointers for Accel, Mag & Gyro logical sensor structures...
int8_t FXLS8471Q_Init(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
int8_t FXLS8471Q_Idle(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define FXLS8471Q_ACCEL_FIFO_SIZE
The sensor_fusion.h file implements the top level programming interface.
#define CHZ
Provides function prototypes for driver level interfaces.
spiSlaveSpecificParams_t slaveParams
SPI specific parameters. Not used for I2C.
#define SPI_ADDR
#define CHX
Used to access X-channel entries in various data data structures.
Definition: sensor_fusion.h:76
void conditionSample(int16_t sample[3])
conditionSample ensures that we never encounter the maximum negative two&#39;s complement value for a 16-...
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
uint16_t isInitialized
Bitfields to indicate sensor is active (use SensorBitFields from build.h)
const registerreadlist_t FXLS8471Q_F_STATUS_READ[]
const registerreadlist_t FXLS8471Q_WHO_AM_I_READ[]
#define F_USING_ACCEL
nominally 0x0001 if an accelerometer is to be used, 0x0000 otherwise
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.
uint16_t addr
I2C address if applicable.
registerreadlist_t FXLS8471Q_DATA_READ[]
const registerwritelist_t FXLS8471Q_IDLE[]